package org.celstec.mobilegame.locatory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.celstec.mobilegame.gamaray.Asset;
import org.celstec.mobilegame.gamaray.Feature;
import org.celstec.mobilegame.gamaray.FeatureImg;
import org.celstec.mobilegame.server.PMF;
import org.jdom.Element;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Locatory {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	public Key key;

	@Persistent
	public int amountOfCollects;
	
	@Persistent
	public String name;
	
	@Persistent
	public int onDistance;
	
	@Persistent
	public String defaultCardImage;
	
	@Persistent
	public String defaultCardImageType;
	
	@Persistent
	public String cardIconImage;
	
	@Persistent
	public String cardIconImageType;
	
	@Persistent
	public String cardDropOffZoneImage;
	
	@Persistent
	public String cardDropOffZoneImageType;
	
	@Persistent
	public String buttonDrop;
	
	@Persistent
	public String buttonDropType;
	
	@Persistent
	public String buttonClose;
	
	@Persistent
	public String buttonCloseType;

	@Persistent(defaultFetchGroup = "true")
	public Set<Key> keysZones;

	@Persistent(defaultFetchGroup = "true")
	public Set<Key> keysLocations;
	

	public static Locatory getObject (Key k){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			return (Locatory) pm.getObjectById(Locatory.class, k);
		} finally {			
			pm.close();

		}
	}
	
//	public Vector<CardDropOffZone> zones = new Vector();
//	
//	public Vector<CardLocation> locations= new Vector();
    
    public HashMap<String, Card> hm;
	
	public Locatory(){
//		Iterator<Key> z = keysZones.iterator();
//		while (z.hasNext()) {
//			Key key = (Key) z.next();
//			zones.add((CardDropOffZone)SubmitGameServiceImpl.getObjectById(key));
//		}
//		Iterator<Key> l = keysZones.iterator();
//		while (z.hasNext()) {
//			Key key = (Key) l.next();
//			locations.add((CardLocation)SubmitGameServiceImpl.getObjectById(key));
//		}
	}
	
	public Locatory(Element metadata){
		if (metadata.getAttributeValue("amountOfCollects")!= null) amountOfCollects = Integer.parseInt(metadata.getAttributeValue("amountOfCollects"));
		if (metadata.getAttributeValue("onDistance")!= null) onDistance = Integer.parseInt(metadata.getAttributeValue("onDistance"));
		if (metadata.getAttributeValue("defaultCardImage")!= null) defaultCardImage = metadata.getAttributeValue("defaultCardImage");
		if (metadata.getAttributeValue("defaultCardImageType")!= null) defaultCardImageType = metadata.getAttributeValue("defaultCardImageType");
		if (metadata.getAttributeValue("cardIconImage")!= null) cardIconImage = metadata.getAttributeValue("cardIconImage");
		if (metadata.getAttributeValue("cardIconImageType")!= null) cardIconImageType = metadata.getAttributeValue("cardIconImageType");
		if (metadata.getAttributeValue("cardDropOffZoneImage")!= null) cardDropOffZoneImage = metadata.getAttributeValue("cardDropOffZoneImage");
		if (metadata.getAttributeValue("cardDropOffZoneImageType")!= null) cardDropOffZoneImageType = metadata.getAttributeValue("cardDropOffZoneImageType");
		if (metadata.getAttributeValue("buttonDrop")!= null) buttonDrop = metadata.getAttributeValue("buttonDrop");
		if (metadata.getAttributeValue("buttonDropType")!= null) buttonDropType = metadata.getAttributeValue("buttonDropType");
		if (metadata.getAttributeValue("buttonClose")!= null) buttonClose = metadata.getAttributeValue("buttonClose");
		if (metadata.getAttributeValue("buttonCloseType")!= null) buttonCloseType = metadata.getAttributeValue("buttonCloseType");
	}
	
	public LocatoryPlay createPlay(String p){
		return new LocatoryPlay(this, p);
	}
	
	public CardLocation getCardLocation(String locationId){
		Iterator<Key> l = keysLocations.iterator();
		while (l.hasNext()) {
			Key key = (Key) l.next();
			CardLocation zone =CardLocation.getObject(key);
			if (locationId.contains(zone.id.toString())) return zone;
		}
//		for (int i = 0; i < this.keysLocations.size(); i++) {
//			CardLocation zone = this.locations.get(i);
//			if (zone.id.equals(locationId)) return zone;
//		}
		return null;
	}
	
	public Card getCard(String cardId){
		Iterator<Key> l = keysLocations.iterator();
		while (l.hasNext()) {
			Key key = (Key) l.next();
			CardLocation zone =CardLocation.getObject(key);
			Card c = Card.getObject(zone.card);
			if (c.cardId.equals(cardId)) return c;

		}
//		for (int i = 0; i < this.locations.size(); i++) {
//			Card c = SubmitGameServiceImpl.getCard(this.locations.get(i).card);
//			if (c.cardId.equals(cardId)) return c;
//		}
		return null;
	}
	

	public String toString(){
		String ret = "Locatory: "+defaultCardImage+" "+defaultCardImageType+"\n";
//		for (int i = 0; i < this.zones.size(); i++) {
//			CardDropOffZone zone = this.zones.get(i);
//			ret += " "+zone.toString() +"\n";
//		}
//		for (int i = 0; i < this.locations.size(); i++) {
//			CardLocation zone = this.locations.get(i);
//			ret += " "+zone.toString() +"\n";
//		}
		return ret;
	}
	public Vector<Asset> getDefaultAssets(){
		Vector<Asset> asset = new Vector<Asset>();
		asset.add(new Asset(defaultCardImageType, defaultCardImage, "defaultCardImage"));
		asset.add(new Asset(cardIconImageType, cardIconImage, "cardIcon"));
		asset.add(new Asset(cardDropOffZoneImageType, cardDropOffZoneImage, "dropOffZone"));
		asset.add(new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score4.png", "scoreClose"));
		asset.add(new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score1.png", "scoreOpen"));
		for (int i=0;i<10;i++){
			asset.add(new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score2-"+i+".png", "scoreNumber_"+i));

		}
//		asset.add();
//		asset.add();
		return asset;
	}
	
//	public Asset getDefaultCardImageAsset(){
//		Asset returnAsset = 
//		return returnAsset;
//	}
//	
//	public Asset getIconCardImageAsset(){
//		Asset returnAsset = ;
//		return returnAsset;
//	}
//	
//	public Asset getCardDropOffZoneAsset(){
//		return  new Asset(cardDropOffZoneImageType, cardDropOffZoneImage, "dropOffZone");
//	}
//	
//	public Asset getScoreCloseAsset(){
////		return  new Asset("PNG", Dimension.baseUrl+"images/score4.png", "scoreClose");
//		return  new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score4.png", "scoreClose");
//
//	}
//	
//	public Asset getScoreOpenAsset(){
////		return  new Asset("PNG", Dimension.baseUrl+"images/score4.png", "scoreClose");
//		return  new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score1.png", "scoreOpen");
//	}
//	public Asset getScoreNumberAsset(int i){
////		return  new Asset("PNG", Dimension.baseUrl+"images/score4.png", "scoreClose");
//		return  new Asset("PNG", "http://sharetec.celstec.org/mobileIm/score2-"+i+".png", "scoreNumber_"+i);
//	}
	
	public Feature getDefaultCardImageFeature(CardLocation cl){
		return new FeatureImg("cl_"+cl.id, "defaultCardImage", cl.lat, cl.lon, "CC", "&amp;cardLocationClick="+cl.id);
	}
}

